package com.zyit.service;

import com.google.gson.Gson;
import com.zyit.DataObject.T_classify;
import com.zyit.DataObject.T_goods;
import com.zyit.service.Impl.ClassifyServiceImpl;
import com.zyit.service.Impl.GoodsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/myselectgoodsservice")
public class MySelectGoodsService extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        GoodsService service = new GoodsServiceImpl();
        String id = req.getParameter("id");

        try {
            System.out.println("id搜索");
            System.out.println(id);
            T_goods tGoods = service.SelectIdGoods(id);
            System.out.println(tGoods);


            // 使用 JSON 序列化库将列表转换为 JSON 字符串
            String json = new Gson().toJson(tGoods);

            // 设置响应的内容类型为 JSON
            resp.setContentType("application/json");

            // 设置响应的字符编码为 UTF-8
            resp.setCharacterEncoding("UTF-8");

            // 获取响应的输出流并将 JSON 写入响应
            resp.getWriter().write(json);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
